/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pagedata.cc
  \date			Aug 2011
  \author		TNick

  \brief		Contains the implementation for PageData class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include    <main.h>

#include	<QObject>
#include    <QNetworkRequest>
#include    <gui/mainwindow.h>
#include    <QCoreApplication>
#include	<QSettings>
#include	<QDir>
#include	<QFile>
#include	<QString>

#include	"pagedata.h"



/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PageData::PageData	(QString the_Url, QString the_displ_name, CC_HDL2DN* pFth)
	: CC_L2DN (the_Url, pFth)
{

	display_name = the_displ_name;
	retr_date = QDate::currentDate();
	retr_time = QTime::currentTime();
    p_next_pg = NULL;

    //        if (s_cache_loc.isEmpty() == false)
    //                {
    //                        cached_file_name = uniqueName();
    //                        if (!cached_file_name.isEmpty())
    //                                { /* get the file */
    //                                        QFile f_tst(cached_file_name);
    //                                        WebPageRetrive wrk;
    //                                        if (f_tst.open(QIODevice::WriteOnly))
    //                                                {
    //                                                        QTextStream out(&f_tst);
    //                                                        out << wrk.get(URL()).toAscii();
    //                                                        f_tst.close();
    //                                                }
    //                                }
    //                }

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PageData::~PageData	(void)
{
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            tmp_cnct::finishReadPage    (void)
{
    QNetworkReply * reply = static_cast<QNetworkReply *>(sender());

    QByteArray data=reply->readAll();
    result = QString(data);
    b_waiting = false;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString             PageData::pageContent      (void)
{

    tmp_cnct    data;
    data.b_waiting = true;
    data.pd = this;

    /* get the web page */
    QNetworkRequest ntw_rq(URL());
    ntw_rq.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 1),
                        QVariant(MainWindow::REQ_WEBPAGE));
    ntw_rq.setRawHeader("User-Agent", "WebChecker");
    ntw_rq.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
                        QNetworkRequest::PreferCache);
    ntw_rq.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 2),
                        qVariantFromValue((void*)&data));
    QNetworkReply * reply = l_manager->get(ntw_rq);
    QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                     g_MW, SLOT(replyError(QNetworkReply::NetworkError)));
    QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                     g_MW, SLOT(replySSLError(QList<QSslError>)));
    QObject::connect(reply, SIGNAL(finished()),
                     &data, SLOT(finishReadPage()));

    for (quint32 i = 0; i < 1000000; i++)
        {
            QCoreApplication::processEvents(
                        QEventLoop::ExcludeUserInputEvents,
                        500);

            if (data.b_waiting == false)
                break;
        }
    if (reply->isRunning())
        {
            reply->abort();
        }
    reply->deleteLater();
    return data.result;

}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
